﻿package Server.AHMJ.SCMJ;

import java.util.HashMap;
import java.util.Map;

import Server.Agent.GameAgent;
import Server.Model.Base.IRoom;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import common.routine.GlobalSetting;
import net.sf.json.JSONObject;

public class MajTableMgr {
	static MajTableMgr __instance = new MajTableMgr();

	public static MajTableMgr instance() {
		return __instance;
	}

	Long m_LastDealTime;
	Long m_LastUpdateTime;

	static Map<Integer, MajTable> m_mapTable = new HashMap<>();// 房间号 . 房间
	Map<Integer, Long> m_mapHeartTime = new HashMap<>();// 心跳时间

	void Init() {
		m_LastDealTime = 0L;
		m_LastUpdateTime = 0L;
	}

	static MajTable FindTable(int _Uid) {
		MajTable it = (MajTable) QpGameMgr.getInstance().getUserRoomByUid(_Uid);
		if (it != null) {
			return it;
		}
		return null;
	}

	void Handle_onHeart(int _Uid) {

	}

	public static void Handle_onConnect(GameAgent agent, String[] vecArg) {
		
		SUBCMD_S_Connect cmd = new SUBCMD_S_Connect();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onJoin(GameAgent agent, String[] vecArg) {
		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onLeave(GameAgent agent, String[] vecArg) {

		Integer _Uid = agent.getUid();
		MajTable pTable = (MajTable) FindTable(_Uid);

		if (pTable != null) {
			pTable.OnUserStandUp(_Uid, true);

			QpGameMgr.getInstance().Handle_OnUserRoom(agent, new JSONObject());

		}

		// m_mapHeartTime.remove(_Uid);

	}

	public static void Handle_onDismissRoom(GameAgent agent, String[] vecArg) {
		Integer _Uid = agent.getUid();
		MajTable tbl = (MajTable) FindTable(_Uid);
		if (tbl != null) {
			QpGameMgr.getInstance().NotifyRemoveUserRoom(tbl);
		}
		if (tbl != null && tbl.m_pIUserItem[0].dwUserID == _Uid && tbl.m_bTableStarted == false) {
			tbl.OnEventGameEnd(0, MajTable.GAME_END_DISMISS);
		}
	}

	// 8JU;AAPAY;BUZHAMA;SIREN;KQGH;QGQB;DDH
	public static MajTable CreateTable(int _Uid, String strParam, boolean isFangGuan) {
		try {
			int byCostGem = 1;
			SHMUser user = dthall.getInstance().getUserInfo(_Uid);
			if (user == null)
				return null;

			if (strParam == null)
				return null;

			String[] vecParam = strParam.split(";");

			if (vecParam.length == 0)
				return null;

			int jushu = 0;
			String moshi = "wubao";
			
			String strRoomDesc = "";
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("8JU")) {
					jushu = 8;
				}
				if (param.equalsIgnoreCase("16JU")) {
					jushu = 16;
				}
				if (param.equalsIgnoreCase("DAIBAO")) {
					moshi = "daibao";
				}
			}

			int isMPHu = 0;
			String payway = "AAPAY";
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("FZPAY")) {
					payway = "FZPAY";
				}
				if (param.equalsIgnoreCase("AAPAY")) {
					payway = "AAPAY";
				}
				if(param.equalsIgnoreCase("CLUBPAY")){
					payway = "CLUBPAY";
				}
			}
			int renShu = 0;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("SANREN")) {
					renShu = 3;
				}
				if (param.equalsIgnoreCase("SIREN")) {
					renShu = 4;
				}

			}
			int maPaiCount =0;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("BUZHAMA")) {
					maPaiCount=0;
				}
				if (param.equalsIgnoreCase("2MA")) {
					maPaiCount = 2;
				}
				if (param.equalsIgnoreCase("4MA")) {
					maPaiCount = 4;
				}
				if (param.equalsIgnoreCase("6MA")) {
					maPaiCount = 6;
				}

			}
			/**
			 * 抢杠全包
			 */
			int qgqb=0;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("QGQB")) {
					qgqb=1;
				} 
			}
			/**
			 * 黄庄黄杠
			 */
			String gangFen = "";
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("GSHZ")) {
					gangFen = "GSHZ";
				}
				if (param.equalsIgnoreCase("GLJY")) {
					gangFen = "GLJY";
				}
			}
			
			int diFenNum = 1;
			int fdFen = 1;
			int maxCost = byCostGem;
			if (payway.equals("AAPAY")) {
				if (jushu == 16) {
					maxCost = 15;
				}
				if (jushu == 8) {
					maxCost = 8;
				}
			}
			if (payway.equals("FZPAY") || payway.equals("CLUBPAY")) {
				if (jushu == 16) {
					maxCost = 60;
				}
				if (jushu == 8) {
					maxCost = 30;
				}
			}
			int realcost = maxCost;
			if(payway.equals("CLUBPAY")){
				maxCost = 0;
			}
			System.out.println("1[" + maxCost + "]1");
			boolean ipLimit = false;
			boolean gpsLimt = false;
			int inZuanShi = 0;
			boolean daidapai = false;
			boolean shuidashuibao = false;
			boolean qianggangzimo = true;
			boolean ganghoux4 = false;
			boolean mingangang = false;
			boolean tianluodihu = false;
			boolean yipaosanxiang = false;
			boolean yunxuzhanzhaung=false;
			boolean buzhunchipai=false;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if(param.equalsIgnoreCase("ipcheck"))
					ipLimit = true;
				else if(param.equalsIgnoreCase("gpscheck"))
					gpsLimt = true;
				else if(param.startsWith("zhunru:")){
					String numStr = param.replace("zhunru:","");
					inZuanShi = Integer.parseInt(numStr);
				}else if(param.equalsIgnoreCase("daidapai"))
					daidapai = true;
				else if(param.equalsIgnoreCase("qiangangzimo"))
					qianggangzimo = true;
				else if(param.equalsIgnoreCase("GHCSB"))
					ganghoux4 = true;
				else if(param.equalsIgnoreCase("YPDX"))
					yipaosanxiang = true;
				else if(param.equalsIgnoreCase("TLDH"))
					tianluodihu = true;
				else if(param.equalsIgnoreCase("mingangang"))
					mingangang = true;
				else if(param.equalsIgnoreCase("SDSCF"))
					shuidashuibao = true;
				else if(param.equalsIgnoreCase("LIANZHUANG"))
					yunxuzhanzhaung = true;	
				else if(param.equalsIgnoreCase("BZCP"))
					buzhunchipai = true;	
				 
			}
			if (inZuanShi != 0 && user.getGem(0) < inZuanShi) {
				// 准入钻石不够
				return null;
			}
			if (jushu > 0 && byCostGem > 0 && payway.length() > 0 && user.getGem(0) >= maxCost) {
				int factor = 0;
				for (int i = 0; i < 1000000; i++) {
                    //生成放好
					int rand_room = GlobalSetting.getInstance().getRandRoomNum();//Constants.rand(100000, 999999, factor);
					//int rand_room=0;
					factor = rand_room;
					MajTable room = (MajTable) QpGameMgr.getInstance().getRoomById(rand_room);
					if (room == null) {
						room = new MajTable();
						room.Init(Constants.INIT_TOTAL);
						room.SetParam(strParam);
						room.SetDesc(strRoomDesc);
						room.m_diFen = diFenNum;
						room.m_MPHu = isMPHu;
						room.m_dwTableID = rand_room;
						room.m_byCostGem = byCostGem;
						room.m_maxCostGem = realcost;
						room.m_payWay = payway;
						room.m_byMaxRound = (int) jushu;
						room.m_Creator = isFangGuan ? _Uid : 0;

						room.m_hqShuiDaShuiBao = shuidashuibao;

						room.m_hqQiangGangZiMo = qianggangzimo;
						room.m_hqGangHoux4 = ganghoux4;
						room.m_hqYiPaoSanXiang = yipaosanxiang;
						room.m_hqTianLuoDiHu = tianluodihu;
						room.m_renShu = renShu;
						room.m_hqIpLimit = ipLimit;
						room.m_hqGpsLimit = gpsLimt;
						room.m_hqInZuanNum = inZuanShi;
						room.m_yunxuzhanzhaung = yunxuzhanzhaung;
						room.m_moshi = moshi;
						/**
						 * 翻码的数量
						 */
						room.m_maPaiCount = maPaiCount;
						// 抢杠全包
						room.m_qgqb = qgqb;
						// 黄庄黄杠
						room.m_gangFen = gangFen;
						QpGameMgr.getInstance().putRoomIntoMap(rand_room, room);

						return room;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	void DismissTable(MajTable pTable) {

		if (pTable == null)
			return;

		QpGameMgr.getInstance().OnDismissRoom((IRoom) pTable);

		{

			m_mapTable.remove(pTable.GetID());
		}

	}

	void OnDeal(float dt) {
		Long tnow_ms = getCurrentTimeMillis();
		if (m_LastDealTime == 0) {
			m_LastDealTime = tnow_ms;
		}

		if (tnow_ms - m_LastDealTime >= 1000) {
			m_LastDealTime = tnow_ms;

		}
	}

	private Long getCurrentTimeMillis() {
		return System.currentTimeMillis();
	}

	public static void OnZuoPai(int id,int seat,int pai[]){
		Integer lUid = id;
		MajTable tbl = (MajTable)FindTable(lUid);
		if (tbl == null) {
			return;
		}else{
			tbl.OnZuoMj(seat,pai);
		}
	}
	
	public static void OnDealPai(int userId,int seat,int pai){
		Integer lUid = userId;
		MajTable tbl = (MajTable)FindTable(lUid);
		if (tbl == null) {
			return;
		}else{
			tbl.OnMJMsg(seat,pai);
		}
	}
	
	public static void OnRecv(GameAgent agent, JSONObject root) {
		String data = root.optString("data");
		String vecArg2[] = data.split("\\|");
		if (vecArg2.length == 0){
			return;
		}
		String subcmd = vecArg2[0];
		switch (subcmd) {
		case "connect":
			Handle_onConnect(agent,vecArg2);
			return;
		case "join":
			Handle_onJoin(agent,vecArg2);
			return;
		case "leave":
			Handle_onLeave(agent,vecArg2);
			return;
		case "dismissroom":
			Handle_onDismissRoom(agent, vecArg2);
			return;
//		case "kick":
//			Handle_onKick(agent, vecArg2);
//			return;
		case "number":
			Handle_onNumber(agent,vecArg2);
			return;
		default:
			break;
		}

		Integer lUid = agent.getUid();
		MajTable tbl = (MajTable) FindTable(lUid);
		if (tbl == null) {
			return;
		}
		if (subcmd.equals("voice")) {
			tbl.Handle_onVoice(lUid, vecArg2);
		} else if (subcmd.equals("reload")) {
			tbl.Handle_onReload(lUid, vecArg2);
		} else if (subcmd.equals("try_dismissroom")) {
			tbl.Handle_onTryDismissRoom(lUid, vecArg2);
		} else if (subcmd.equals("agree_dismissroom")) {
			tbl.Handle_onAgreeDismissRoom(lUid, vecArg2);
		} else if (subcmd.equals("refuse_dismissroom")) {
			tbl.Handle_onRefuseDismissRoom(lUid, vecArg2);
		}

		boolean printLog = true;
		if (subcmd.equals("ready")) {
			tbl.Handle_onReady(lUid, vecArg2);
		} else if (subcmd.equals("robot")) {
			tbl.Handle_onRobot(lUid, vecArg2);
		} else if (subcmd.equals("biaoqing")) {
			tbl.Handle_onBiaoQing(lUid, vecArg2);
		} else if (subcmd.equals("yuyin")) {
			tbl.Handle_onYuYin(lUid, vecArg2);
		} else if (subcmd.equals("chat")) {
			tbl.Handle_onChat(lUid, vecArg2);
		} else if (subcmd.equals("voice")) {
			tbl.Handle_onVoice(lUid, vecArg2);
		} else if (subcmd.equals("voicemsg")) {
			printLog = false;
			tbl.Handle_onVoiceMsg(lUid, vecArg2);
		} else if (subcmd.equals("text")) {
			tbl.Handle_onText(lUid, vecArg2);
		} else if (subcmd.equals("try_mopai")) {
			tbl.Handle_onTryMoPai(lUid, vecArg2);
		} else if (subcmd.equals("mopai")) {
			tbl.Handle_onMoPai(lUid, vecArg2);
		} else if (subcmd.equals("chupai")) {
			tbl.Handle_onChuPai(lUid, vecArg2);
		} else if (subcmd.equals("chipai")) {
			tbl.Handle_onChiPai(lUid, vecArg2);
		} else if (subcmd.equals("pengpai")) {
			tbl.Handle_onPengPai(lUid, vecArg2);
		} else if (subcmd.equals("gangpai")) {
			tbl.Handle_onGangPai(lUid, vecArg2);
		} else if (subcmd.equals("guopai")) {
			tbl.Handle_onGuoPai(lUid, vecArg2);
		} else if (subcmd.equals("hupai")) {
			tbl.Handle_onHuPai(lUid, vecArg2);
		} else if (subcmd.equals("baozi")) {
			tbl.Handle_onBaoZiPai(lUid, vecArg2);
		} else if (subcmd.equals("inBack")) {
			tbl.Handle_InBack(lUid, vecArg2);
		} else if (subcmd.equals("inFore")) {
			tbl.Handle_InFore(lUid, vecArg2);
		} else if (subcmd.equals("clientIp")) {
			tbl.Handle_ClientIp(lUid, vecArg2);
		} else if (subcmd.equals("clientGps")) {
			tbl.Handle_ClientGps(lUid, vecArg2);
		} else if (subcmd.equals("kick")){
			tbl.Handle_Kick(lUid, vecArg2);
		}
		if (printLog)
			System.out.println("Msg=" + root.toString());
	}

	private static void Handle_onNumber(GameAgent agent, String[] vecArg2) {
		 Integer number=Integer.parseInt(vecArg2[1])+1;
		  SUBCMD_S_Number ssm=new SUBCMD_S_Number();
		  ssm.number=number;
		  String data =ssm.getJsonData();
		  agent.SendMessage(data);
	}

	  

	private static void Handle_onKick(GameAgent agent, String[] vecArg2) {
		/*
		Integer _Uid = Integer.parseInt(vecArg2[1]);
		
		GameAgent agent2  =  GameAgent.find_client_by_user_id(_Uid);
		MajTable pTable = (MajTable)FindTable(_Uid);
		
		if (pTable != null)
		{
			pTable.OnUserStandUp(_Uid); 
		}
		
		SUBCMD_S_Kick cmd = new SUBCMD_S_Kick();
		cmd.m_lUid = _Uid;
		//{"mjmsg":"kick|31448|0|0","command":"mjmsg"}
		String sData = cmd.getJsonData();
		agent2.Broadcast(sData);
		*/
	}
}
